﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jemgine.Engine.Scripts
{
    [Category("System"), Node]
    public class OnlyOnceEvery : Data.ScriptNode
    {
        public int Ticks { get; set; }

        private int LastFire = -1;

        public OnlyOnceEvery()
        {
            Ticks = 1;
            AddInput("In", null);
            AddOutput("Out", null);
        }

        public override void FireTerminal(int InputID, Data.ScriptPayload Payload)
        {
            if (LastFire == -1 || LastFire + Ticks < Sim.TickCount)
            {
                LastFire = Sim.TickCount;
                OutputTerminals[0].Fire(Payload);
            }
        }


    }

    [Category("System"), Node]
    public class Timer : Data.ScriptNode
    {
        public int Ticks { get; set; }

        public Timer()
        {
            AddInput("In", null);
            AddOutput("Out", null);
        }

        public override void FireTerminal(int InputID, Data.ScriptPayload Payload)
        {
            OutputTerminals[0].Fire(Payload, Ticks);
        }
    }

    [Category("System"), Node]
    public class Once : Data.ScriptNode
    {
        bool HasFired = false;

        public Once()
        {
            AddInput("In", null);
            AddOutput("Out", null);
        }

        public override void FireTerminal(int InputID, Data.ScriptPayload Payload)
        {
            if (HasFired) return;
            HasFired = true;

            OutputTerminals[0].Fire(Payload);
        }
    }
}
